// pages/address-edit/index.js
Page({
  data: {
    addressId: '',
    name: '',
    phone: '',
    region: ['请选择', '请选择', '请选择'],
    address: '',
    isDefault: false,
    isEdit: false,
    loading: false
  },

  onLoad: function (options) {
    // 如果传入了地址ID，则是编辑模式
    if (options.id) {
      this.setData({
        addressId: options.id,
        isEdit: true
      });
      
      wx.setNavigationBarTitle({
        title: '编辑收货地址'
      });
      
      this.loadAddressDetail(options.id);
    } else {
      wx.setNavigationBarTitle({
        title: '新增收货地址'
      });
    }
  },

  // 加载地址详情
  loadAddressDetail: function (addressId) {
    wx.showLoading({ title: '加载中...' });
    
    // 模拟API请求，实际开发中应替换为真实的API调用
    setTimeout(() => {
      // 模拟获取地址详情
      const addressDetail = this.getMockAddressDetail(addressId);
      
      if (addressDetail) {
        this.setData({
          name: addressDetail.name,
          phone: addressDetail.phone,
          region: [addressDetail.province, addressDetail.city, addressDetail.district],
          address: addressDetail.address,
          isDefault: addressDetail.isDefault
        });
      }
      
      wx.hideLoading();
    }, 1000);
  },

  // 输入框内容变化处理
  onInputChange: function (event) {
    const { field } = event.currentTarget.dataset;
    this.setData({
      [field]: event.detail
    });
  },

  // 地区选择器变化处理
  bindRegionChange: function (event) {
    this.setData({
      region: event.detail.value
    });
  },

  // 默认地址开关变化处理
  onSwitchChange: function (event) {
    this.setData({
      isDefault: event.detail
    });
  },

  // 保存地址
  saveAddress: function () {
    // 表单验证
    if (!this.validateForm()) {
      return;
    }
    
    this.setData({ loading: true });
    
    // 构建地址数据
    const addressData = {
      id: this.data.addressId || new Date().getTime().toString(),
      name: this.data.name,
      phone: this.data.phone,
      province: this.data.region[0],
      city: this.data.region[1],
      district: this.data.region[2],
      address: this.data.address,
      isDefault: this.data.isDefault
    };

    console.log(addressData)
    
    // 模拟API请求，实际开发中应替换为真实的API调用
    setTimeout(() => {
      this.setData({ loading: false });
      
      wx.showToast({
        title: this.data.isEdit ? '地址已更新' : '地址已添加',
        icon: 'success'
      });
      
      // 返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }, 1000);
  },

  // 表单验证
  validateForm: function () {
    const { name, phone, region, address } = this.data;
    
    if (!name.trim()) {
      wx.showToast({
        title: '请输入收货人姓名',
        icon: 'none'
      });
      return false;
    }
    
    if (!phone.trim()) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return false;
    }
    
    // 简单的手机号验证
    const phoneReg = /^1[3-9]\d{9}$/;
    if (!phoneReg.test(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      });
      return false;
    }
    
    if (region[0] === '请选择' || region[1] === '请选择' || region[2] === '请选择') {
      wx.showToast({
        title: '请选择所在地区',
        icon: 'none'
      });
      return false;
    }
    
    if (!address.trim()) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      });
      return false;
    }
    
    return true;
  },

  // 删除地址
  deleteAddress: function () {
    if (!this.data.isEdit) return;
    
    wx.showModal({
      title: '提示',
      content: '确定要删除此地址吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟删除地址API调用
          wx.showLoading({ title: '处理中...' });
          
          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({
              title: '地址已删除',
              icon: 'success'
            });
            
            // 返回上一页
            setTimeout(() => {
              wx.navigateBack();
            }, 1500);
          }, 1000);
        }
      }
    });
  },

  // 模拟获取地址详情数据
  getMockAddressDetail: function (addressId) {
    const mockAddressList = [
      {
        id: '1',
        name: '张三',
        phone: '13800138000',
        province: '广东省',
        city: '深圳市',
        district: '南山区',
        address: '科技园南路XX号XX大厦X座XXX室',
        isDefault: true
      },
      {
        id: '2',
        name: '李四',
        phone: '13900139000',
        province: '广东省',
        city: '广州市',
        district: '天河区',
        address: '天河路XX号XX广场X座XXX室',
        isDefault: false
      },
      {
        id: '3',
        name: '王五',
        phone: '13700137000',
        province: '北京市',
        city: '北京市',
        district: '朝阳区',
        address: '朝阳路XX号XX大厦X座XXX室',
        isDefault: false
      }
    ];
    
    return mockAddressList.find(item => item.id === addressId);
  }
});